﻿package view
{
	import controller.GameController;
	
	import fl.transitions.Tween;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.Product;
	
	public class Shelf extends MovieClip
	{
		private var upperRightStartPosition:Object = new Object();
		private var shelfDistance:uint = Config.ShelfDistancePixX;
		private var boxDistancePixY = Config.BoxDistancePixY;
		private var productWidthInShelf = Config.ProductWidthInShelf;
		private var productHeightInShelf = Config.ProductHeightInShelf;
		private var productDistanceX = Config.ProductDistanceX;
		private var amountOfProductsFlying:int = 0;
		
		private var productWrappers:Array = new Array();
		private var tmpWrappers:Array = new Array();
		
		public function Shelf(products:Array):void
		{
			this.upperRightStartPosition.x = Config.UpperRightShelfCornerX;
			this.upperRightStartPosition.y = Config.UpperRightShelfCornerY;
			this.fillShelf(products);
		}
		
		private function mixArray (array:Array):Array
		{
			return  array.sort (function () {return Math.floor(Math.random()*array.length-1);});
		}
		
		
		private function fillShelf(products:Array):void
		{
			products = this.mixArray(products);
			this.amountOfProductsFlying = products.length;
		//	trace("Produkte am Anfang: " + this.amountOfProductsFlying);
			var x:uint = this.upperRightStartPosition.x;
			var y:uint = this.upperRightStartPosition.y;
			
			for (var i = 0; i <= products.length - 1; i++) {
				//	trace(i+1,x, y);
				this.productWrappers.push(new ProductWrapper(products[i], x, y));
				
				var productCount = i + 1;
				if (productCount % 6 == 0) { //jump down to beginning of next line
					x = this.upperRightStartPosition.x;
					y = + this.productHeightInShelf
						+ this.boxDistancePixY
						+ y;
				} else if (productCount % 2 == 0 && productCount % 6 != 0) { //jump right to next shelf
					x = x
						+ this.shelfDistance
						+ this.productWidthInShelf ;					
				} else {
					x = x 
						+ this.productWidthInShelf
						+ this.productDistanceX;
				}
				this.addChild(this.productWrappers[i]);	//add current wrapper sprite to shelf 
			}
			tmpWrappers = productWrappers;
		}
		
		public function letNextProductFly(gameController:GameController):void 
		{
		//	trace("Aufruf des nächsten Produktes" + tmpWrappers.length);
			if(tmpWrappers.length != 0){
				var track:int = (Math.floor((Math.random()*3)));
				var productNum:int = (Math.floor((Math.random()*tmpWrappers.length)));
				var product:ProductWrapper = tmpWrappers[productNum];
				//make sure that flying product is in front of others
		//		trace("udn", product.getProduct().getId(), productNum, tmpWrappers.length);
				
				if(!gameController.getGameState()){
					this.setChildIndex(product, this.numChildren - 1);
				}
				
				tmpWrappers.splice(productNum,1);
			//	trace(tmpWrappers.length);
				product.animateTo(product, Config.productTrack[track], gameController);
			}
		}
		
		public function addEventBtnListener(e:String, listener:Function) {
			this.addEventListener(e, listener);
		}
		
		public function putItBack(productWrapper:ProductWrapper, gameController:GameController):void
		{			
	//		trace("***********", productWrapper.getProduct().getId(), tmpWrappers.length);
			tmpWrappers.push(productWrapper);
			this.amountOfProductsFlying++;
			tmpWrappers = this.mixArray(tmpWrappers);
	//		trace("tmpWrappers: " + tmpWrappers.length);
			if(tmpWrappers.length == 1){
				// methode aus shelf aufrufen
				letNextProductFly(gameController);
				
			}
			
		}
		
		public function getRemainingProducts():int
		{
			return this.amountOfProductsFlying;
		}
	}
}